package com.h2cloud;

import lombok.Data;

import java.util.*;

@Data
class Model{
    int age;
    String name;

}
public class test {

    public static void main(String[] args) {
        List<Model> models = new ArrayList<>();
        Model model1 = new Model();
        model1.setAge(300);
        model1.setName("a");
        models.add(model1);

        Model model2 = new Model();
        model2.setAge(500);
        model2.setName("c");
        models.add(model2);

        Model model3 = new Model();
        model3.setAge(100);
        model3.setName("b");
        models.add(model3);


        Set<Model> set = new TreeSet<>(Comparator.comparing(Model::getAge));
        set.addAll(models);

        System.out.println("+++++++ sout set ++++++++");
        for (Model contract : set) {
            System.out.println(contract.getName() + " " + contract.getAge());
        }

        System.out.println("+++++++ sout ArrayList ++++++++");
        ArrayList<Model> toDoTasks = new ArrayList<>(set);
        for (Model contract : toDoTasks) {
            System.out.println(contract.getName() + " " + contract.getAge());
        }


        System.out.println("-----排序前-----");
        // 排序前
        for (Model contract : models) {
            System.out.println(contract.getName() + " " + contract.getAge());
        }

        System.out.println("-----排序后，根据age排序-----");
        Collections.sort(models, Comparator.comparing(Model::getAge));
        // 排序后
        for (Model model : models) {
            System.out.println(model.getName() + " " + model.getAge());
        }

        System.out.println("-----排序后，根据age排倒序-----");
        Collections.sort(models, Comparator.comparing(Model::getAge).reversed());
        // 排序后
        for (Model model : models) {
            System.out.println(model.getName() + " " + model.getAge());
        }

        System.out.println("-----排序后，根据name排序-----");
        Collections.sort(models, Comparator.comparing(Model::getName));
        // 排序后
        for (Model model : models) {
            System.out.println(model.getName() + " " + model.getAge());
        }
    }
}
